package com.alex.demo.springtest.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @Version 1.0
 * @Author Alex
 * @Created Dec 2022/8/18 18:00
 * @Description
 *              <p>
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

	/**
	 * 该方法在每一个bean对象的初始化方法调用之前回调（BeanPostProcessor前置处理）
	 */
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if (bean instanceof MyJavaBean) {
			// System.out.println("BeanPostProcessor，对象" + beanName + "调用初始化方法之前的数据： " + bean.toString());
			System.out.println("调用 BeanPostProcessor 前置处理方法 postProcessBeforeInitialization");
		}
		return bean;
	}

	/**
	 * 该方法在每个bean对象的初始化方法调用之后被回调（BeanPostProcessor后置处理）
	 */
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if (bean instanceof MyJavaBean) {
			// System.out.println("BeanPostProcessor，对象" + beanName + "调用初始化方法之后的数据：" + bean.toString());
			System.out.println("调用 BeanPostProcessor 后置处理方法 postProcessAfterInitialization");
		}
		return bean;
	}
}